package com.gxks.lhs.security;

import com.gxks.lhs.entity.User;
import com.gxks.lhs.security.UserDetails.SysUserDetails;
import com.gxks.lhs.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class SysUserDetailsService implements UserDetailsService {

    @Autowired
    private IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
        User user = userService.getUserByPhone(phone);
//        User user = userService.getUserByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户名或密码错误: " + phone);
        }
        SysUserDetails sysUserDetails = new SysUserDetails(user);
        return sysUserDetails;
    }
}